package com.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bean.Material;
import com.bean.Permission;
import com.bean.User;
import com.service.MaterialService;
import com.service.PermissionService;
import com.util.TimeUtil;

/**
 * @author author : 张鹏
 * @date createTime：2018年3月20日 上午11:14:47
 * @Description: 该类的功能描述
 */
@Controller
@RequestMapping("permission")
public class PermissionController {
	@Autowired
	private PermissionService permissionService;

	/**
	 * 获得所有权限
	 * 
	 * @return
	 */
	@RequestMapping("getAllPermissionAjax")
	@ResponseBody
	public List<Permission> getAllPermissionAjax() {
		return permissionService.getAllPermission();
	}

	@RequestMapping("deletePermissionByPrimaryKeyAjax")
	@ResponseBody
	public int deletePermissionByPrimaryKeyAjax(int permissionId) {
		return permissionService.deletePermissionByPrimaryKey(permissionId);
	}

	@RequestMapping("getPermissionByPrimaryKeyReturnTo")
	public String getPermissionByPrimaryKeyReturnTo(int permissionId,
			String returnTo, HttpServletRequest request) {
		request.setAttribute("permission",
				permissionService.getPermissionByPrimaryKey(permissionId));
		return returnTo;
	}

	@RequestMapping("updatePermissionAjax")
	@ResponseBody
	public int updatePermissionAjax(Permission p) {
		return permissionService.updatePermission(p);
	}
	
	@RequestMapping("createNewPermissionAjax")
	@ResponseBody
	public int createNewPermissionAjax(Permission p) {
		return permissionService.createNewPermission(p);
	}


	/**
	 * 根据角色得到权限
	 * 
	 * @param roleId
	 * @param returnTo
	 * @param request
	 * @return
	 */
	@RequestMapping("getPermissionByRoleIdReturnTo")
	public String getPermissionByRoleIdReturnTo(int roleId, String returnTo,
			HttpServletRequest request) {
		request.setAttribute("permissionList",
				permissionService.getAllPermission());
		request.setAttribute("roleId", roleId);
		request.setAttribute("rolePermissionList",
				permissionService.getPermissionByRoleId(roleId));
		request.setAttribute("classificationList",
				permissionService.getPermissionClassification());
		return returnTo;
	}

	/**
	 * 修改角色权限
	 * 
	 * @param roleId
	 * @param request
	 * @return
	 */
	@RequestMapping("updatePermissionByRoleIdAjax")
	@ResponseBody
	public int updatePermissionByRoleIdAjax(int roleId,
			HttpServletRequest request) {
		return permissionService.createRolePermission(request, roleId);
	}

}
